/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function calTime(){
    var currentETime = new Date();
    var eTime = currentETime.getTime();
    $('#responseTime').html(eTime - sTime);
}
var sTime;
function startTime(){
    var currentSTime = new Date();
    sTime = currentSTime.getTime();
    $('#responseTime').html("<img src=\"./images/loader.gif\" />");
}

 


function loadUserTable(){
    
    startTime();
    $("#showTable").html("<img src=\"./images/loader.gif\" />");
    $("#showTable").load("UserTable").hide().fadeIn('slow');
    calTime();
  

}

jQuery(document).ready(function(){
    jQuery("#formID").validationEngine('attach', {
        onValidationComplete: function(form, status){
            if(status){
                var oksumit=false;
                var gid1=$("#gid").val();
                var userrole1=$("#userrole").val();

                $.post(
                    "UserManager",
                    {
                        action:"checkuser",
                        gid:gid1
                    },
                    function(data){
                        if (data.success){
                            oksumit=confirm("user have already in database,Do you want to edit user role? ");
                            if(oksumit) {
                            $.blockUI();
                                $.post(
                                    "UserManager",
                                    {
                                        action: "update",
                                        gid: gid1 ,
                                        userrole:userrole1
                                    },
                                    function(data){
                                        if (data.success){
                                            alert("Success to edit user role ");
                                            loadUserTable();
                                            $("#gid").val("");

                                        }
                                        else
                                            alert("Error can't edit data ");
                                    },
                                    "json"

                                    );

                            }
                        }
                        else{
                            $.blockUI();
                            $.post(
                                "UserManager?action=adduser",
                                $("#formID").serialize(),
                                function(data){
                                    if (data.success){
                                        alert("success to add user ");
                                        loadUserTable();
                                        $("#gid").val("");
                                        
                                    }
                                    else
                                        alert("Error can't insert data ");
                                },
                                "json"

                                );
                        }
                    },
                    "json"

                    );
                         
              
            }
        }
    });
});

function deleteUser(gid1){
    if(confirm('Confirm to delete user : '+gid1)==true)
    {
        $.post(
            "UserManager",
            {
                action: "delete",
                gid: gid1

            },
            function(data){
                if (data.success){
                    alert("success to delete user ");
                    loadUserTable();
                }
                else
                    alert("Error can't delete user ");
            },
            "json"

            );
    }
}
function editUser(gid,userrole){

    $("#gid").val(gid);
    $("#userrole").val(userrole).focus();

}

